import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class TEST {
    @Test
    public void test() throws Exception {
        // 把classpath上的文件转换成输入流
        InputStream inputStream = TEST.class.getClassLoader().getResourceAsStream("data.txt");

        // 使用哪个IO对象比较合适  BufferedReader
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        List<Address> addresses = new ArrayList<Address>();

        String line = null;

        SqlSession sqlSession = getConnect();
        IAddressDao addressDao = sqlSession.getMapper(IAddressDao.class);

        int i = 1;
        while ( ( line=bufferedReader.readLine() )!=null  ){

            String[] split = line.split(",");
//            System.out.println(Arrays.toString(split));
            Address address = new Address(split[0], split[1], split[2], split[3]);
            addresses.add(address);
//            System.out.println(address);
            if (addresses.size() == 1000){
                addressDao.insert(addresses);
                sqlSession.commit();

                addresses = new ArrayList<Address>();
                System.out.println(i);
                i++;
            }



//

//            if (i==30){
//                break;
//            }
        }


        bufferedReader.close();
//        iAddressDao.insert(addresses);
//        sqlSession.commit();
        System.out.println("--执行完毕--");
    }

    public SqlSession getConnect() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = factory.openSession();
        return sqlSession;
    }
}
